package cn.bugstack.xfg.dev.tech.trigger;

import cn.bugstack.xfg.dev.tech.api.IAiService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

@RestController()
@CrossOrigin("*")
@RequestMapping("/api/v1/ollama/")
@Slf4j
public class OllamaController implements IAiService {
    @Resource
    private OllamaChatClient chatClient;
    /**
     * http://localhost:8090/api/v1/ollama/generate?model=deepseek-r1:1.5b&message=1+1
     */
    @GetMapping("/generate")
    @Override
    public ChatResponse generate(String model, String message) {
        log.info("model: {}, message: {}", model, message);
        return chatClient.call(new Prompt(message, OllamaOptions.create().withModel(model)));
    }
    /**
     * http://localhost:8090/api/v1/ollama/generate_stream?model=deepseek-r1:1.5b&message=hi
     */
    @GetMapping("/generate_stream")
    @Override
    public Flux<ChatResponse> generateStream(String model, String message) {
        log.info("model: {}, message: {}", model, message);
        return chatClient.stream(new Prompt(message, OllamaOptions.create().withModel(model)));
    }
}
